﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Data;
using Pipes.Data.Table;

namespace Pipes.UnitTest
{
  /// <summary>
  ///This is a test class for ExtensionTest and is intended
  ///to contain all ExtensionTest Unit Tests
  ///</summary>
  [TestClass()]
  public class VisTableJsonWriterTest
  {
    private TestContext testContextInstance;

    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext
    {
      get
      {
        return testContextInstance;
      }
      set
      {
        testContextInstance = value;
      }
    }

    #region Additional test attributes
    // 
    //You can use the following additional attributes as you write your tests:
    //
    //Use ClassInitialize to run code before running the first test in the class
    //[ClassInitialize()]
    //public static void MyClassInitialize(TestContext testContext)
    //{
    //}
    //
    //Use ClassCleanup to run code after all tests in a class have run
    //[ClassCleanup()]
    //public static void MyClassCleanup()
    //{
    //}
    //
    //Use TestInitialize to run code before running each test
    //[TestInitialize()]
    //public void MyTestInitialize()
    //{
    //}
    //
    //Use TestCleanup to run code after each test has run
    //[TestCleanup()]
    //public void MyTestCleanup()
    //{
    //}
    //
    #endregion


    /// <summary>
    ///A test for WriteTo
    ///</summary>
    [TestMethod()]
    public void VisTableJsonWriter_Simple()
    {
      var table = new DataTable();
      table.Columns.Add("A", typeof(string)).Caption = "NEW A";
      table.Columns.Add("B", typeof(double)).Caption = "B-label";
      table.Columns.Add("C", typeof(DateTime)).Caption = "C-label";

      var row = table.NewRow();
      row.ItemArray = new object[] { "a", 1.0, new DateTime(2008, 2, 28, 0, 31, 26) };
      table.Rows.Add(row);

      row = table.NewRow();
      row.ItemArray = new object[] { "b", 2.0, new DateTime(2008, 3, 30, 0, 31, 26) };
      table.Rows.Add(row);

      row = table.NewRow();
      row.ItemArray = new object[] { "c", 3.0, new DateTime(2008, 4, 30, 0, 31, 26) };
      table.Rows.Add(row);

      var reader = new Pipes.Data.DataTableReader(table);
      var writer = new Google.VisJsonTextWriter(new System.IO.StringWriter());
      writer.UseUtcDateTime = false;
      var tableWriter = new Google.VisTableJsonWriter(writer);
      reader.WriteTo(tableWriter);

      var expected = "{cols:[{id:'A',label:'NEW A',type:'string'},{id:'B',label:'B-label',type:'number'},{id:'C',label:'C-label',type:'datetime'}],rows:[{c:[{v:'a'},{v:1},{v:new Date(2008, 1, 28, 0, 31, 26)}]},{c:[{v:'b'},{v:2},{v:new Date(2008, 2, 30, 0, 31, 26)}]},{c:[{v:'c'},{v:3},{v:new Date(2008, 3, 30, 0, 31, 26)}]}]}";
      Assert.AreEqual(expected, tableWriter.ToString());
    }

    /// <summary>
    ///A test for WriteTo
    ///</summary>
    [TestMethod()]
    public void VisTableJsonWriter_WithQuery()
    {
      var table = new DataTable();
      table.Columns.Add("A", typeof(string)).Caption = "NEW A";
      table.Columns.Add("B", typeof(double)).Caption = "B-label";
      table.Columns.Add("C", typeof(DateTime)).Caption = "C-label";

      var row = table.NewRow();
      row.ItemArray = new object[] { "a", 1.0, new DateTime(2008, 2, 28, 0, 31, 26) };
      table.Rows.Add(row);

      row = table.NewRow();
      row.ItemArray = new object[] { "b", 2.0, new DateTime(2008, 3, 30, 0, 31, 26) };
      table.Rows.Add(row);

      row = table.NewRow();
      row.ItemArray = new object[] { "c", 3.0, new DateTime(2008, 4, 30, 0, 31, 26) };
      table.Rows.Add(row);

      var reader = new Pipes.Data.DataTableReader(table);
      var writer = new Google.VisTableJsonWriter();
      reader.SelectColumns((c) => c.Name != "C").Where((r) => (double)(r.Item("B")) < 3.0).WriteTo(writer);

      var expected = "{cols:[{id:'A',label:'NEW A',type:'string'},{id:'B',label:'B-label',type:'number'}],rows:[{c:[{v:'a'},{v:1}]},{c:[{v:'b'},{v:2}]}]}";
      Assert.AreEqual(expected, writer.ToString());
    }
  }
}
